import { createRouter, createWebHistory} from 'vue-router'
import cookie from "js-cookie";

const Index = ()=>import('../views/Index.vue') //主页
const Loign = ()=>import('../views/Loign.vue') //登录
const Error = ()=>import('../views/404.vue') //404页面

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect:'/index'
    },
    {
      path: '/index',
      name:"Index",
      redirect:"/index/dashboard", 
      component:Index,
      children:[
        {name:"redirect",
         path:"redirect/:backpath(.*)",
         component:()=>import("@/views/redirect/Index.vue"),  //页面刷新组件
         meta:{
          title:''
         }
        },
        {
          path:"userinfo",
          name:"userinfo",
          component:()=>import("@/views/admin/UserInfo.vue"), //头像详细资料
          meta:{
            title:'personalinformation'
          }
        },
        {
          path:"changepassword",
          name:"changepassword",
          component:()=>import("@/views/admin/ChangePassword.vue"),//修改密码
          meta:{
            title:"changepassword"
          }
        }
      ]
    },
    {
      path:'/login',
      name:"Loign",
      component:Loign
    },
    {
      path:'/404',
      name:"Error",
      component:Error
    }
  ]
})
//路由拦截守卫
router.beforeEach((to,from)=>{
   //接收token
   const token = cookie.get("token");
   //判断是否有token和排除登录页面
   if(!token&&to.name!="Loign"){
      router.replace({path:"/login",query:{back:to.fullPath}});
      return false
   }
   return true
})

export default router
